package com.apes.fn.fico.invoiceApply.model;

import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.JpaUtil;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductGroup;
import com.apes.scm.masterdata.product.model.ProductUom;
import com.apes.scm.masterdata.product.model.ServeType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.fico.invoiceApply.model
 * @ClassName: InvoiceApplyItem
 * @Author: pyb
 * @Description: 发票申请明细
 * @CreateTime: 2021-04-08 17:24
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = DirectInvoiceApplyItem.CONST_TABLE_NAME)
public class DirectInvoiceApplyItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "acc_direct_invoice_apply_item";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "发票申请明细")
    @Comment("明细编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "invoice_apply_id", referencedColumnName = "id")
    @Comment("发票申请")
    private DirectInvoiceApply invoiceApply;

    @Column(name = "mark", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("收回标记")
    private Boolean mark = false;

    @Column(name = "origin", length = 30)
    @Comment("源单据")
    private String origin;

    @Column(name = "item_id", length = 30)
    @Comment("源明细行号")
    private String itemId;

    @Column(name = "origin_business_type", length = 32)
    @Comment("源单据类型")
    private String originBusinessTypeName;

    @Column(name = "source", length = 30)
    @Comment("来源单据")
    private String source;

    @Column(name = "source_business_type", length = 32)
    @Comment("来源单据类型")
    private String sourceBusinessTypeName;

    @Column(name = "source_item_id", length = 30)
    @Comment("来源单明细单号")
    private String sourceItemId;

    @Column(name = "car_license", length = 64)
    @Comment("车牌号码")
    private String carLicense;

    @Comment("车主信息")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_expand_id", referencedColumnName = "id")
    private FnCustomerExpand customerExpand;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品")
    private Product product;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位")
    private ProductUom uom;


    @Column(name = "product_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("销售数量")
    private double productQty = 0;

    @Column(name = "price_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("销售金额")
    private double priceTotal = 0;

    @Comment("折扣金额")
    @Column(name = "discount_amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double discountAmount = 0;


    @Comment("实际发票金额")
    @Column(name = "actual_invoiced_Amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double actualInvoicedAmount = 0;

    @Comment("已开票金额")
    @Column(name = "invoiced_Amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double invoicedAmount = 0;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("税率")
    private Tax tax;

    @Column(name = "specifications", length = 200)
    @Comment("规格")
    private String specifications;

    @Column(name = "model", length = 200)
    @Comment("型号")
    private String model;


    @ManyToOne(fetch = FetchType.LAZY)
    @Comment("商品组")
    @JoinColumn(name = "product_group", referencedColumnName = "id")
    private ProductGroup productGroup;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "serveType_id", referencedColumnName = "id")
    @Comment("服务类型")
    private ServeType serveType;

    @Column(name = "invoice_number", length = 256)
    @Comment("发票号码")
    private String invoiceNumber;

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note;

    //
//
//    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
//    @Comment("含税单价")
//    private double priceUnit = 0;
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != JpaUtil.getDomainClass((ApesBean) o)) return false;
        DirectInvoiceApplyItem item = (DirectInvoiceApplyItem) o;
        return Objects.equals(id, item.getId());
    }

}
